// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出注册方法 ============================== //

export default (Vue, importsList) => {
  // 导入样式
  require('./CSS')

  // 添加原始配置
  importsList.unshift(require.context('./CONFIG'))

  // 合并基础配置
  importsList.forEach((imports) => {
    imports.keys().forEach((path) => {
      const baseJs = /^\.\/BASE\/([A-Z]\w+)\.js$/.exec(path)
      const name = (baseJs || [])[1]
      if (name) CORE.merge(CORE.WEB.BASE, { [name]: imports(path).default })
    })
  })

  // 合并组件配置
  importsList.forEach((imports) => {
    imports.keys().forEach((path) => {
      const brotherJs = /^\.\/([A-Z]\w+)\.js$/.exec(path)
      const folderJs = /^\.\/[a-z]+\/([A-Z]\w+)\.js$/.exec(path)
      const name = (brotherJs || folderJs || [])[1]
      if (name) CORE.merge(CORE.WEB, { [name]: imports(path).default })
    })
  })

  // 注册组件
  const imports = require.context('./', true, /index\.vue/)
  imports.keys().forEach((path) => {
    const webVue = /^\.\/Web[A-Z]\w+\/index\.vue$/.exec(path)
    const easyVue = /^\.\/Easy[A-Z]\w+\/index\.vue$/.exec(path)
    const rollVue = /^\.\/Roll[A-Z]\w+\/index\.vue$/.exec(path)
    const folderVue = /^\.\/[a-z]+\/[A-Z]\w+\/index\.vue$/.exec(path)
    if (webVue || easyVue || rollVue || folderVue) {
      const component = imports(path).default
      Vue.component(component.name, component)
    }
  })
}
